package com.oa.modules.approvalsec1.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.modules.approvalsec1.entity.EnterpriseEntity;
import com.oa.modules.approvalsec1.service.EnterpriseService;
import com.oa.modules.sys.controller.AbstractController;
import com.oa.modules.sys.entity.SysUserRoleEntity;
import com.oa.modules.sys.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;


/**
 * 企业表
 *
 * @author yangwenzhi
 * @email 785297226@qq.com
 * @date 2022-01-13 10:37:13
 */
@RestController
@RequestMapping("enterprise")
@Api(tags = "企业表相关接口")
public class EnterpriseController extends AbstractController {
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = enterpriseService.queryPage(params);
        List<EnterpriseEntity> list = (List<EnterpriseEntity>) page.getList();
        for (EnterpriseEntity entity : list) {
            if(entity.getStatus()==1){
                LambdaQueryWrapper<SysUserRoleEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(SysUserRoleEntity::getUserId, entity.getUserId());
                SysUserRoleEntity sysUserRoleEntity = sysUserRoleService.getOne(lambdaQueryWrapper);
                entity.setRoleId(sysUserRoleEntity.getRoleId());
            }
        }
        page.setList(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") String id) {
        EnterpriseEntity enterprise = enterpriseService.getById(id);
        LambdaQueryWrapper<SysUserRoleEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRoleEntity::getUserId, enterprise.getUserId());
        SysUserRoleEntity sysUserRoleEntity = sysUserRoleService.getOne(lambdaQueryWrapper);
        enterprise.setRoleId(sysUserRoleEntity.getRoleId());
        return R.ok().put("enterprise", enterprise);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody EnterpriseEntity enterprise) {
        enterprise.setId(AppUtils.getUUID());
        enterprise.setCreateDate(new Date());
        enterprise.setCreateOper("1");
        enterprise.setStatus(1);
        enterpriseService.save(enterprise);
        List<Long> list = new ArrayList<>();
        list.add(enterprise.getRoleId());
        sysUserRoleService.saveOrUpdate(enterprise.getUserId(), list);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody EnterpriseEntity enterprise) {
        enterprise.setUpdateDate(new Date());
        enterprise.setUpdateOper(getUserId().toString());
        enterpriseService.updateById(enterprise);
        List<Long> list = new ArrayList<>();
        list.add(enterprise.getRoleId());
        sysUserRoleService.saveOrUpdate(enterprise.getUserId(), list);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids) {
        enterpriseService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
